<template>
  <el-dialog v-if="visible" :title="info.title" :visible.sync="visible" append-to-body center width="760px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="120px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="22">
          <el-form-item label="项目名称" prop="pro">
            <ProSelectObject v-model="form.pro" :id="form.pro_id" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户名称" prop="header">
            <el-input v-model="form.header" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="申请金额" prop="money">
            <el-input v-model="form.money" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发票类型" prop="type">
            <el-select v-model="form.type" size="small" style="width: 100%">
              <el-option
                v-for="item in invoice_types"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="税率" prop="rate">
            <el-select v-model="form.rate" size="small" style="width: 100%">
              <el-option
                v-for="item in rates"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="税号" prop="tax_num">
            <el-input v-model="form.tax_num" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开户行" prop="bank">
            <el-input v-model="form.bank" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="银行账号" prop="account">
            <el-input v-model="form.account" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地址" prop="addr">
            <el-input v-model="form.addr" size="small" clearable />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="24">
          <el-form-item label="附件上传" prop="files">
            <Upload v-model="form.files" />
          </el-form-item> -->
        <!-- </el-col> -->
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" :rows="4" size="small" clearable style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item style="height: 37px; overflow: hidden" label="PO单号">
            <span v-if="form.pro.no">{{ "PO"+form.pro.no }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item style="height: 37px; overflow: hidden" label="订单金额">
            <span v-if="project.id">{{ project.trading_price }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item style="height: 37px; overflow: hidden" label="已开票金额">
            <span v-if="invoicing.tm">{{ invoicing.tm }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item style="height: 37px; overflow: hidden" label="未开票金额">
            <span v-if="invoicing.wm">{{ invoicing.wm }}</span>
          </el-form-item>
        </el-col>
        <!-- <el-col class="mt10" :span="24">
          <el-form-item label="抄送" prop="ccs">
            <VerifyCC v-model="form.ccs" :ccs="ccs" />
          </el-form-item>
        </el-col> -->
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleBeforeTo()">提 交</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import ProSelectObject from '@/myComponents/ProSelectObject'
// import VerifyCC from '@/myComponents/VerifyCC'
import Upload from '@/myComponents/Upload'
import svf from '../../util/svf'
import { queryTotalMoney } from '@/api/finance/invoice'
import { queryPro } from '@/api/project'
import { queryCusById } from '@/api/customer'

export default {
  components: { ProSelectObject, Upload },
  extends: svf,
  data() {
    const vnull = (rule, value, callback) => {
      if (JSON.stringify(value) === '{}') {
        callback(new Error(rule.message))
      } else {
        callback()
      }
    }
    return {
      approve_key: 'PROCESS_PRO_INVOICING',
      invoicing: {},
      project: {},
      rules: {
        pro: [{ required: true, trigger: 'blur', message: '项目名称不可为空', validator: vnull }],
        money: [{ required: true, trigger: 'blur', message: '申请金额不可为空' }],
        type: [{ required: true, trigger: 'blur', message: '发票类型不可为空' }],
        rate: [{ required: true, trigger: 'blur', message: '税率不可为空' }],
        header: [{ required: true, trigger: 'blur', message: '发票抬头不可为空' }],
        tax_num: [{ required: true, trigger: 'blur', message: '税号不可为空' }],
        bank: [{ required: true, trigger: 'blur', message: '开户行不可为空' }],
        account: [{ required: true, trigger: 'blur', message: '银行账号不可为空' }],
        phone: [{ required: true, trigger: 'blur', message: '联系电话不可为空' }],
        addr: [{ required: true, trigger: 'blur', message: '地址不可为空' }],
        files: [{ required: true, trigger: 'blur', message: '附件不可为空' }]
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        if(this.info.type && this.info.type === 'again'){
          this.initApply(this.info.edit)
        }else{
          this.form = this.info.edit
        }
        this.$set(this.form, 'pro', {})
        this.$set(this, 'invoicing', {})
        this.$set(this, 'project', {})
        // this.queryCcs()
      }
    },
    'form.pro': {
      handler(n, o) {
        if (this.form.pro.key) {
          this.queryPro()
        }
      },
      deep: true
    },
    'project.ctm_id': {
      handler(n, o) {
        if (n && n !== '') {
          queryCusById({ id: n }).then(response => {
            let info = response.info || {}
            this.$set(this.form, 'account', info.account)
            this.$set(this.form, 'header', info.name)
            this.$set(this.form, 'bank', info.bank)
            this.$set(this.form, 'addr', info.addr)
            this.$set(this.form, 'tax_num', info.num)
          }).catch(err => {
            this.$message.error(err.msg)
          })
        }
      },
      deep: true
    }
  },
  mounted() {
  },
  methods: {
    queryTotalMoney() {
      queryTotalMoney({ pro_id: this.form.pro.key }).then(response => {
        const tm = response.info.total ? response.info.total : 0
        const wm = this.project.trading_price - tm
        this.$set(this, 'invoicing', { tm: tm + '', wm: wm + '' })
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    queryPro() {
      queryPro({ id: this.form.pro.key }).then(response => {
        this.project = response.pro
        this.queryTotalMoney()
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    handleBeforeTo() {
      this.validate().then(() => {
        const e = {}
        // e.files = this.form.files
        e.approve_key = this.approve_key
        e.des = '项目名称：' + this.form.pro.value + '    备注：' + this.form.remark
        e.ccs = this.form.ccs
        e.forms = JSON.stringify(this.createForms([
          ['PO单号', 'PO' + this.form.pro.no, '', ''],
          ['项目名称', this.form.pro.value, this.form.pro.key, 'pro_id'],
          ['客户名称', this.form.header, this.form.header, 'header'],
          ['开户行', this.form.bank, this.form.bank, 'bank'],
          ['银行账号', this.form.account, this.form.account, 'account'],
          ['税号', this.form.tax_num, this.form.tax_num, 'tax_num'],
          ['申请金额', this.form.money, this.form.money, 'money'],
          ['税率', this.form.rate, this.form.rate, 'rate'],
          ['地址', this.form.addr, this.form.addr, 'addr'],
          ['发票类型', this.form.type, this.form.type, 'type'],
          ['备注', this.form.remark, this.form.remark, 'remark']
        ]))
        this.handleCreate(e)
      })
    }
  }
}
</script>

<style>
</style>
